package com.example.dangerouscargodriver.ui.activity;

import androidx.annotation.Nullable;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiju.bean.CityBean;
import com.baiju.bean.ProvinceBean;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.widget.LoadingDialog;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class PersonalModifyLineInfoActivity extends HttpRequestActivity implements View.OnClickListener {

    private static final int START_ACTIVITY_FOR_LOAD_POSITION_ONE = 0;
    private static final int START_ACTIVITY_FOR_LOAD_POSITION_TWO = 1;
    private static final int START_ACTIVITY_FOR_LOAD_POSITION_THREE = 2;

    private static final int START_ACTIVITY_FOR_UNLOAD_POSITION_ONE = 3;
    private static final int START_ACTIVITY_FOR_UNLOAD_POSITION_TWO = 4;
    private static final int START_ACTIVITY_FOR_UNLOAD_POSITION_THREE = 5;
    private static final String TAG = "ModifyLineInfoActivity";

    private ActionBar mActionBar = null;

    private LinearLayout mLoadPositionOne = null;
    private LinearLayout mLoadPositionTwo = null;
    private LinearLayout mLoadPositionThree = null;

    private LinearLayout mUnLoadPositionOne = null;
    private LinearLayout mUnLoadPositionTwo = null;
    private LinearLayout mUnLoadPositionThree = null;

    private ImageView mIvLoadPositionOne = null;
    private TextView mTvLoadPositionOne = null;
    private ImageView mIvLoadPositionTwo = null;
    private TextView mTvLoadPositionTwo = null;
    private ImageView mIvLoadPositionThree = null;
    private TextView mTvLoadPositionThree = null;
    private ImageView mIvUnLoadPositionOne = null;
    private TextView mTvUnLoadPositionOne = null;
    private ImageView mIvUnLoadPositionTwo = null;
    private TextView mTvUnLoadPositionTwo = null;
    private ImageView mIvUnLoadPositionThree = null;
    private TextView mTvUnLoadPositionThree = null;

    //地址选择存储
    private ProvinceBean mSelectLoadProvinceOne = null;
    private CityBean mSelectLoadCityOne = null;

    private ProvinceBean mSelectLoadProvinceTwo = null;
    private CityBean mSelectLoadCityTwo = null;

    private ProvinceBean mSelectLoadProvinceThree = null;
    private CityBean mSelectLoadCityThree = null;

    private ProvinceBean mSelectUnLoadProvinceOne = null;
    private CityBean mSelectUnLoadCityOne = null;

    private ProvinceBean mSelectUnLoadProvinceTwo = null;
    private CityBean mSelectUnLoadCityTwo = null;

    private ProvinceBean mSelectUnLoadProvinceThree = null;
    private CityBean mSelectUnLoadCityThree = null;

    private Button mActionCommit = null;

    private String mSrid = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(PersonalModifyLineInfoActivity.class);
        setContentView(R.layout.activity_personal_modify_line_info);

        mLoadPositionOne = findViewById(R.id.layout_load_position_one);
        mLoadPositionOne.setOnClickListener(this);
        mLoadPositionTwo = findViewById(R.id.layout_load_position_two);
        mLoadPositionTwo.setOnClickListener(this);
        mLoadPositionThree = findViewById(R.id.layout_load_position_three);
        mLoadPositionThree.setOnClickListener(this);

        mUnLoadPositionOne = findViewById(R.id.layout_unload_position_one);
        mUnLoadPositionOne.setOnClickListener(this);
        mUnLoadPositionTwo = findViewById(R.id.layout_unload_position_two);
        mUnLoadPositionTwo.setOnClickListener(this);
        mUnLoadPositionThree = findViewById(R.id.layout_unload_position_three);
        mUnLoadPositionThree.setOnClickListener(this);

        mIvLoadPositionOne = findViewById(R.id.iv_load_position_one);
        mTvLoadPositionOne = findViewById(R.id.tv_load_position_one);
        mIvLoadPositionTwo = findViewById(R.id.iv_load_position_two);
        mTvLoadPositionTwo = findViewById(R.id.tv_load_position_two);
        mIvLoadPositionThree = findViewById(R.id.iv_load_position_three);
        mTvLoadPositionThree = findViewById(R.id.tv_load_position_three);

        mIvUnLoadPositionOne = findViewById(R.id.iv_unload_position_one);
        mTvUnLoadPositionOne = findViewById(R.id.tv_unload_position_one);
        mIvUnLoadPositionTwo = findViewById(R.id.iv_unload_position_two);
        mTvUnLoadPositionTwo = findViewById(R.id.tv_unload_position_two);
        mIvUnLoadPositionThree = findViewById(R.id.iv_unload_position_three);
        mTvUnLoadPositionThree = findViewById(R.id.tv_unload_position_three);

        mActionCommit = findViewById(R.id.btn_company_add_line);
        mActionCommit.setOnClickListener(this);
        mActionBar = findViewById(R.id.actionBarPersonalModifyLineInfo);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initLineInfo();
    }

    private void initLineInfo(){
        mSelectLoadCityOne = (CityBean) getIntent().getParcelableExtra("start");
        if(mSelectLoadCityOne != null){
            mTvLoadPositionOne.setText(mSelectLoadCityOne.getName());
            mIvLoadPositionOne.setImageResource(R.drawable.ic_delete_yellow);
        }

        mSelectUnLoadCityOne = (CityBean) getIntent().getParcelableExtra("end");
        if(mSelectUnLoadCityOne != null){
            mTvUnLoadPositionOne.setText(mSelectUnLoadCityOne.getName());
            mIvUnLoadPositionOne.setImageResource(R.drawable.ic_delete);
        }

        mSrid = getIntent().getStringExtra("srid");
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V2_SR_ADD) {
                Log.d(TAG, "onResponse: " + response);
                dismissLoadingDialog();
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    ToastUtils.showLongToast(this, "已保存！");
                    finish();
                }
                else{
                    ToastUtils.showLongToast(this, "修改线路失败！");
                }
            }
            else{
                ToastUtils.showLongToast(this, "修改线路失败！");
            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V2_SR_ADD) {
            dismissLoadingDialog();
            ToastUtils.showLongToast(this, "修改线路失败！");
        }
    }

    private void executePublishLineInfo(){
        HashMap<String, String> header = new HashMap<>();
        header.put("token", AuthController.Instance().getToken());
        HashMap<String, String> param = new HashMap<>();
        String from_id = "";
        if(mSelectLoadCityOne != null){
            from_id += mSelectLoadCityOne.getId() + ",";
        }
        if(mSelectLoadCityTwo != null){
            from_id += mSelectLoadCityTwo.getId() + ",";
        }
        if(mSelectLoadCityThree != null){
            from_id += mSelectLoadCityThree.getId() + ",";
        }
        if(from_id.length() > 0){
            from_id = from_id.substring(0, from_id.length() - 1);
        }
        param.put("from_id", from_id);

        String to_id = "";
        if(mSelectUnLoadCityOne != null){
            to_id += mSelectUnLoadCityOne.getId() + ",";
        }
        if(mSelectUnLoadCityTwo != null){
            to_id += mSelectUnLoadCityTwo.getId() + ",";
        }
        if(mSelectUnLoadCityThree != null){
            to_id += mSelectUnLoadCityThree.getId() + ",";
        }
        if(to_id.length() > 0){
            to_id = to_id.substring(0, to_id.length() - 1);
        }
        param.put("to_id", to_id);
        if(mSrid != null) {
            param.put("srid", mSrid);
        }

        Log.d(TAG, "param: " + param.toString());

		sendHttpRequest(
				IHttpRequest.RequestMethod.POST_METHOD,
				RemoteAPI.REQUEST_V2_SR_ADD,
				header,
				param,
				RemoteAPICmd.REQUEST_V2_SR_ADD);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.layout_load_position_one){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectLoadProvinceOne);
            intent.putExtra("city", mSelectLoadCityOne);
            startActivityForResult(intent, START_ACTIVITY_FOR_LOAD_POSITION_ONE);
        }
        else if(v.getId() == R.id.layout_load_position_two){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectLoadProvinceTwo);
            intent.putExtra("city", mSelectLoadCityTwo);
            startActivityForResult(intent, START_ACTIVITY_FOR_LOAD_POSITION_TWO);
        }
        else if(v.getId() == R.id.layout_load_position_three){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectLoadProvinceThree);
            intent.putExtra("city", mSelectLoadCityThree);
            startActivityForResult(intent, START_ACTIVITY_FOR_LOAD_POSITION_THREE);
        }
        else if(v.getId() == R.id.layout_unload_position_one){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectUnLoadProvinceOne);
            intent.putExtra("city", mSelectUnLoadCityOne);
            startActivityForResult(intent, START_ACTIVITY_FOR_UNLOAD_POSITION_ONE);
        }
        else if(v.getId() == R.id.layout_unload_position_two){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectUnLoadProvinceTwo);
            intent.putExtra("city", mSelectUnLoadCityTwo);
            startActivityForResult(intent, START_ACTIVITY_FOR_UNLOAD_POSITION_TWO);
        }
        else if(v.getId() == R.id.layout_unload_position_three){
            Intent intent = new Intent(this, ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectUnLoadProvinceThree);
            intent.putExtra("city", mSelectUnLoadCityThree);
            startActivityForResult(intent, START_ACTIVITY_FOR_UNLOAD_POSITION_THREE);
        }
        else if(v.getId() == R.id.btn_company_add_line){
            showLoadingDialog();
            executePublishLineInfo();
        }
        else{
            /**/
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == START_ACTIVITY_FOR_LOAD_POSITION_ONE){
            if(data != null) {
                mSelectLoadCityOne = (CityBean) data.getParcelableExtra("city");
                mSelectLoadProvinceOne = (ProvinceBean) data.getParcelableExtra("province");
                if(mSelectLoadCityOne.getId().equals("0")){
                    mTvLoadPositionOne.setText(mSelectLoadProvinceOne.getName());
                }
                else {
                    mTvLoadPositionOne.setText(mSelectLoadCityOne.getName());
                }
                mIvLoadPositionOne.setImageResource(R.drawable.ic_delete_yellow);
            }
        }
        else if(requestCode == START_ACTIVITY_FOR_LOAD_POSITION_TWO){
            if(data != null) {
                mSelectLoadCityTwo = (CityBean) data.getParcelableExtra("city");
                mSelectLoadProvinceTwo = (ProvinceBean) data.getParcelableExtra("province");
                if(mSelectLoadCityTwo.getId().equals("0")){
                    mTvLoadPositionTwo.setText(mSelectLoadProvinceTwo.getName());
                }
                else {
                    mTvLoadPositionTwo.setText(mSelectLoadCityTwo.getName());
                }
                mIvLoadPositionTwo.setImageResource(R.drawable.ic_delete_yellow);
            }
        }
        else if(requestCode == START_ACTIVITY_FOR_LOAD_POSITION_THREE){
            if(data != null) {
                mSelectLoadCityThree = (CityBean) data.getParcelableExtra("city");
                mSelectLoadProvinceThree = (ProvinceBean) data.getParcelableExtra("province");
                if(mSelectLoadCityThree.getId().equals("0")){
                    mTvLoadPositionThree.setText(mSelectLoadProvinceThree.getName());
                }
                else {
                    mTvLoadPositionThree.setText(mSelectLoadCityThree.getName());
                }
                mIvLoadPositionThree.setImageResource(R.drawable.ic_delete_yellow);
            }
        }
        else if(requestCode == START_ACTIVITY_FOR_UNLOAD_POSITION_ONE){
            if(data != null) {
                mSelectUnLoadCityOne = (CityBean) data.getParcelableExtra("city");
                mSelectUnLoadProvinceOne = (ProvinceBean) data.getParcelableExtra("province");
                if(mSelectUnLoadCityOne.getId().equals("0")){
                    mTvUnLoadPositionOne.setText(mSelectUnLoadProvinceOne.getName());
                }
                else {
                    mTvUnLoadPositionOne.setText(mSelectUnLoadCityOne.getName());
                }
                mIvUnLoadPositionOne.setImageResource(R.drawable.ic_delete);
            }
        }
        else if(requestCode == START_ACTIVITY_FOR_UNLOAD_POSITION_TWO){
            if(data != null) {
                mSelectUnLoadCityTwo = (CityBean) data.getParcelableExtra("city");
                mSelectUnLoadProvinceTwo = (ProvinceBean) data.getParcelableExtra("province");
                if(mSelectUnLoadCityTwo.getId().equals("0")){
                    mTvUnLoadPositionTwo.setText(mSelectUnLoadProvinceTwo.getName());
                }
                else {
                    mTvUnLoadPositionTwo.setText(mSelectUnLoadCityTwo.getName());
                }
                mIvUnLoadPositionTwo.setImageResource(R.drawable.ic_delete);
            }
        }
        else if(requestCode == START_ACTIVITY_FOR_UNLOAD_POSITION_THREE){
            if(data != null) {
                mSelectUnLoadCityThree = (CityBean) data.getParcelableExtra("city");
                mSelectUnLoadProvinceThree = (ProvinceBean) data.getParcelableExtra("province");
                if(mSelectUnLoadCityThree.getId().equals("0")){
                    mTvUnLoadPositionThree.setText(mSelectUnLoadProvinceThree.getName());
                }
                else {
                    mTvUnLoadPositionThree.setText(mSelectUnLoadCityThree.getName());
                }
                mIvUnLoadPositionThree.setImageResource(R.drawable.ic_delete);
            }
        }
        else{
            /**/
        }
    }

}